home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / r3sim.arc / SIM_R3.C < prev    next >
C/C++ Source or Header  |  1989-07-08  |  9KB  |  373 lines

  1. /***********************
  2.     Program: 68705R3 simulator
  3.     File: sim_r3 - main program / user interface.
  4.     Notes: This program is written in 'plain vanilla' C and should easily
  5.     port to any computer that has rudimentary control of its cursor
  6.     position.  Using defines in SIM.H, it has been successfully run
  7.     on IBM-PC compiled with Microsoft C 5.0, and on an Atari 520ST
  8.     compiled with Mark Williams C 3.0.  The 520ST emulates a DEC VT52
  9.     for cursor control.  It should be easy to adapt this code to drive
  10.     other terminals/emulations, ie, VT100, ADM-3, etc.
  11.     Changes:
  12.     1.21 - 8 July 1989 - Added clear of external interrupt latch to
  13.         sim_reset() in instr.c - This is to agree with 68705 reset.
  14.         The only symptom of this in rev 1.2 would be if someone
  15.         issues 'I' (interrupt) and then 'X' (reset) without
  16.         servicing the interrupt, the interrupt would persist. 
  17.  
  18. ***********************/
  19. #include <stdio.h>
  20.  
  21. #define SIMR3_C
  22. #include "sim.h"
  23.  
  24. #ifdef MSC
  25. #include <graph.h>
  26. #include <conio.h>
  27. #include <ctype.h>
  28. #endif
  29.  
  30. int display_enabled, log_enabled, promptline_full;
  31. int external_intr, timer_external_input;
  32. int disassemble = 0;
  33. long int cycle_count;
  34.  
  35. int trace_ena = 0;
  36. int breakaddr;
  37.  
  38. FILE *logfile;
  39.  
  40. int sim_halt()
  41. /* called from here and from instr.c on invalid op code */
  42. {
  43.     trace_ena = 0;
  44.     settextposition( 1, 65);
  45.     printf( "Halted ");
  46.     fflush(stdout);
  47. }
  48.  
  49. main()
  50. {
  51.     int exitflag = 0;
  52.     char cmdbuff[100];
  53.     char rspbuff[100];
  54.  
  55.     display_enabled = 1;
  56.     log_enabled = 1;
  57.     logfile = fopen( "SIM_R3.LOG", "w");
  58.     promptline_full = 0;
  59.     external_intr = 0;       /* latched /INT ; 0 = no INT latched */
  60.     timer_external_input = 0;
  61.  
  62.     clearscreen();
  63.     settextposition(1, 1);
  64.     printf( "68705R3 Simulator V1.21  8-July-1989\n" );
  65.     printf(
  66. "R - disp/chg regs    L file - load Sx & reset    E - toggle TIE  G - Go\n");
  67.     printf(
  68. "T - trace on/off    B addr - Break at address (go)  I - /INT      S - Step\n");
  69.     printf(
  70. "X - Reset    M addr - Mem view/mod    Z - Disassem    H - Halt      Q - Quit\n");
  71.     settextposition( PROMPTLINE+1, 1);
  72.     printf(
  73. "______________________________________________________________________________"
  74.     );
  75.  
  76.     settextposition( 1, 50);
  77.     printf("Disp+log ON ");
  78.     sim_halt();
  79.     clearline(PROMPTLINE);
  80.     settextposition( PROMPTLINE, 1);
  81.  
  82.  
  83.     /* here is the main loop */
  84.     while (!exitflag)
  85.     {
  86.     /* Check for keyboard input */
  87.     if (kbhit())
  88.     {
  89.         char cmdchar;
  90.         char tempchar;
  91.  
  92.         clearline(PROMPTLINE);
  93.         settextposition( PROMPTLINE, 1);
  94.         fgets( cmdbuff, 80, stdin);
  95.         cmdchar = tolower( cmdbuff[0]);
  96.         if (cmdchar == 'r')
  97.         {
  98.         sprintf( rspbuff,
  99.         "PC= %03x, A= %02x, X= %02x, SP= %03x, CCR=%02x (",
  100.             pgm_model.pc, pgm_model.a, pgm_model.x, pgm_model.sp,
  101.             pgm_model.ccr);
  102.         if (pgm_model.ccr & CC_H)
  103.             strcat( rspbuff, "H");
  104.         if (pgm_model.ccr & CC_I)
  105.             strcat( rspbuff, "I");
  106.         if (pgm_model.ccr & CC_N)
  107.             strcat( rspbuff, "N");
  108.         if (pgm_model.ccr & CC_Z)
  109.             strcat( rspbuff, "Z");
  110.         if (pgm_model.ccr & CC_C)
  111.             strcat( rspbuff, "C");
  112.         strcat( rspbuff, ")  Change?(reg) ");
  113.         showprompt( rspbuff);
  114.         fgets( cmdbuff, 80, stdin);
  115.         tempchar = tolower( cmdbuff[0]);
  116.         if (tempchar == 'p')
  117.         {
  118.             showprompt("Enter new PC: ");
  119.             fgets( cmdbuff, 80, stdin);
  120.             sscanf( cmdbuff, "%x", &pgm_model.pc);
  121.         }
  122.         if (tempchar == 's')
  123.         {
  124.             showprompt("Enter new SP: ");
  125.             fgets( cmdbuff, 80, stdin);
  126.             sscanf( cmdbuff, "%x", &pgm_model.sp);
  127.         }
  128.         if (tempchar == 'a')
  129.         {
  130.             showprompt("Enter new A: ");
  131.             fgets( cmdbuff, 80, stdin);
  132.             sscanf( cmdbuff, "%x", &pgm_model.a);
  133.         }
  134.         if (tempchar == 'x')
  135.         {
  136.             showprompt("Enter new X: ");
  137.             fgets( cmdbuff, 80, stdin);
  138.             sscanf( cmdbuff, "%x", &pgm_model.x);
  139.         }
  140.         if (tempchar == 'c')
  141.         {
  142.             showprompt("Enter new CCR (in hex): ");
  143.             fgets( cmdbuff, 80, stdin);
  144.             sscanf( cmdbuff, "%x", &pgm_model.ccr);
  145.         }
  146.         clearline( PROMPTLINE);
  147.         }
  148.         if (cmdchar == 'l')
  149.         load( &cmdbuff[1]); /* skip command char */
  150.         if (cmdchar == 'e')
  151.         {
  152.         timer_external_input = (timer_external_input)? 0 : 1;
  153.         sprintf(rspbuff, "Timer ext input is now %1d",
  154.                                     timer_external_input);
  155.         showprompt( rspbuff);
  156.         }
  157.         if (cmdchar == 't')
  158.         {
  159.         display_enabled = (display_enabled) ? 0 : 1;
  160.         if (display_enabled)
  161.         {
  162.             showprompt("Enable logfile also? (Y/N): ");
  163.             fgets( cmdbuff, 80, stdin);
  164.             settextposition( 1, 50);
  165.             if (cmdbuff[0] != 'n' && cmdbuff[0] != 'N')
  166.             {
  167.             printf("Disp+log ON ");
  168.             log_enabled = 1;
  169.             }
  170.             else
  171.             {
  172.             printf("Display ON  ");
  173.             log_enabled = 0;
  174.             }
  175.         }
  176.         else
  177.         {
  178.             settextposition( 1, 50);
  179.             printf("Disp+log OFF");
  180.         }
  181.         }
  182.         if (cmdchar == 'b' || cmdchar == 'g' || cmdchar == 's')
  183.         {
  184.         if (cmdchar == 'b')
  185.             sscanf( &cmdbuff[1], "%x", &breakaddr);
  186.         if (cmdchar != 's')
  187.             trace_ena = 1;
  188.         else
  189.             trace_ena = 2; /* step */
  190.         settextposition( 1, 65);
  191.         printf( "RUNNING");
  192.         }
  193.         if (cmdchar == 'i')
  194.         {
  195.             external_intr = 1;
  196.         showprompt("External interrupt latched");
  197.         }
  198.         if (cmdchar == 'x')
  199.         sim_reset();
  200.         if (cmdchar == 'm')
  201.         {
  202.         int addr, data, endm, numcvts;
  203.  
  204.         addr = 0x10;  /* Default */
  205.         sscanf( &cmdbuff[1], "%x", &addr);
  206.  
  207.         endm = 0;
  208.         while (!endm)
  209.         {
  210.             if (addr > 4095 || addr < 0)
  211.             addr = 0x10; /* Wrap to RAM = default */
  212.             data = sim_read( addr);
  213.             sprintf( rspbuff, "%03x %02x      ", addr, data);
  214.             showprompt(rspbuff);
  215.             settextposition( PROMPTLINE, 8); 
  216.             cmdbuff[0] = '\n';
  217.             fgets( cmdbuff, 80, stdin);
  218.             if (cmdbuff[0] != '\n')
  219.             {
  220.             numcvts = sscanf( cmdbuff, "%x", &data);
  221.             if (numcvts == 1)
  222.                 sim_writef( addr, data);
  223.             else
  224.                 endm = 1;
  225.             }
  226.             addr++;
  227.         }
  228.         clearline( PROMPTLINE);
  229.         }
  230.         if (cmdchar == 'h')
  231.         {
  232.             sim_halt();
  233.         }
  234.         if (cmdchar == 'q')
  235.         exitflag = 1;
  236.         if (cmdchar == 'z')
  237.         {
  238.         struct program_model saveregs;
  239.         int ic, addr, savelogena, savedispena;
  240.  
  241.         saveregs = pgm_model;
  242.         disassemble = 1;
  243.         savelogena = log_enabled;
  244.         savedispena = display_enabled;
  245.         log_enabled = 0;
  246.         display_enabled = 1;
  247.  
  248.         delete_line( PROMPTLINE+2);
  249.         settextposition( 24,1);
  250.         printf(" *** Disassembly follows ****");
  251.  
  252.         if (cmdbuff[1] != '\n')
  253.         {
  254.             ic = sscanf(&cmdbuff[1], "%x", &addr);
  255.             if (ic == 1)
  256.             pgm_model.pc = addr;
  257.         }
  258.  
  259.         for (ic = 0; ic < 15; ic++)
  260.         {
  261.             sim_instr();
  262.         }
  263.         delete_line( PROMPTLINE+2);
  264.         settextposition( 24,1);
  265.         printf (" *** End disassembly ****");
  266.  
  267.         disassemble = 0;
  268.         pgm_model = saveregs;
  269.         log_enabled =  savelogena;
  270.         display_enabled = savedispena;
  271.         }
  272.         if (cmdchar == '\n')
  273.         {
  274.         settextposition( PROMPTLINE+1, 1);
  275.         printf(
  276. "______________________________________________________________________________"
  277.         );
  278.         };
  279.         settextposition(PROMPTLINE, 1);
  280.     } /* end if kbhit */
  281.  
  282.     if (trace_ena)
  283.     {
  284.         instr_or_intr(); /* do an instruction */
  285.         if (pgm_model.pc == breakaddr)
  286.         {
  287.         sim_halt();
  288.         trace_ena = 0;
  289.         showprompt("BREAKPOINT reached");
  290.         }
  291.         if (trace_ena == 2)
  292.         {
  293.         sim_halt();
  294.         trace_ena = 0;
  295.         }
  296.     }
  297.     } /* end while !exitflag */
  298.     sim_halt();
  299.     fclose( logfile);
  300.     showprompt("Logfile closed - normal exit");
  301.     settextposition(25,1);
  302.     exit( 0);
  303. }
  304.  
  305.  
  306.     
  307.  
  308. showprompt( buff)
  309. char buff[];
  310. {
  311.     if (promptline_full)
  312.     clearline( PROMPTLINE);
  313.     settextposition( PROMPTLINE, 1);
  314.     printf( buff);
  315.     fflush(stdout);
  316.     promptline_full = 1;
  317. }
  318.  
  319. clearline( linenum)
  320. int linenum;
  321. {
  322.     settextposition( linenum, 1);
  323. #ifdef MSC
  324.     printf(
  325. "                                        ");
  326.     settextposition( linenum, 1);
  327. #endif
  328. #ifdef VT52
  329.     settextposition( linenum, 1);
  330.     printf( "\033K");    /* escape K - clear cursor pos through end of line */
  331.     settextposition( linenum, 1);
  332. #endif
  333.     if (linenum == PROMPTLINE)
  334.     promptline_full = 0;
  335. }
  336.  
  337. settextposition( line, col)
  338. int line,col;
  339. {
  340. #ifdef MSC
  341.     _settextposition( line, col);       /* Microsoft C 5.0 and up */
  342. #endif
  343. #ifdef VT52
  344.     printf("\033Y%c%c", 31+line, 31+col); /* escape Y row col  */
  345.     /* row and col are 0 to max with an offset of 32 */
  346.     fflush( stdout);
  347. #endif
  348. }
  349.  
  350. clearscreen()
  351. {
  352. #ifdef MSC
  353.     _clearscreen( _GCLEARSCREEN );       /* Microsoft C 5.0 and up */
  354. #endif
  355. #ifdef VT52
  356.     printf("\033E");  /* escape E - Clear & Home */
  357. #endif
  358. }
  359.  
  360. #ifdef VT52
  361. /* Assume this is for ATARI ST, which does VT52 emulation */
  362. /* We must supply our own version for the Microsoft C routine kbhit() */
  363.  
  364. #include <osbind.h>
  365.  
  366. int kbhit()
  367. {
  368.     return( Cconis() ); /* Check console input status */
  369. }
  370. #endif
  371.  
  372. /******************** end of file sim_r3.c ***************************/
  373.